package mmc.ui;

import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glColor4f;

import java.util.ArrayList;

import org.lwjgl.opengl.GL11;

import mmc.Client;
import mmc.resource.Sprite2D;

public class Radar extends Component {
	private Client client;
	private Sprite2D circle;	
	
	public Radar(float pos_x, float pos_y, float size_x, float size_y, Client client) {
		super(pos_x, pos_y, size_x, size_y);
		this.client = client;
		circle = new Sprite2D("mmc/resource/data/circle.png");
	}

	@Override
	public void render() {
		glColor3f(0, 0.5f, 1); //blau-grun
		
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x, y);
		GL11.glVertex2f(x+width, y);
		GL11.glVertex2f(x+width, y+height);
		GL11.glVertex2f(x, y+height);
		GL11.glEnd();
		
		
		//draw triangle for self-position
		if(client.getScene() != null && client.getScene().player != null)
		{
			FloatPoint p1 = new FloatPoint(x+width/2, y+height/2);
			FloatPoint p2 = new FloatPoint(p1.x + (float)(Math.sin(Math.toRadians(360-client.getScene().player.yaw))*0.01f), p1.y + (float)(Math.cos(Math.toRadians(360-client.getScene().player.yaw))*0.01f));
			FloatPoint p7 = new FloatPoint(p1.x - (float)(Math.sin(Math.toRadians(360-client.getScene().player.yaw))*0.005f), p1.y - (float)(Math.cos(Math.toRadians(360-client.getScene().player.yaw))*0.005f));
			FloatPoint p3 = new FloatPoint(p2.x-p7.x, p2.y-p7.y);
			FloatPoint p4 = new FloatPoint(p3.y, -p3.x);
			FloatPoint p5 = new FloatPoint(p7.x+p4.x/3 , p7.y+p4.y/3);
			FloatPoint p6 = new FloatPoint(p7.x-p4.x/3 , p7.y-p4.y/3);
			
			//draw enemies
			ArrayList<float[]> list = client.getScene().getNearPlayers();
			
			for (float[] fs : list) {
				float posx = p1.x + (client.getScene().player.x - fs[0])*0.01f;
				float posz = p1.y + (client.getScene().player.z - fs[1])*0.01f;
				
				posx = clamp(posx, x, x+width);
				posz = clamp(posz, y, y+height);
					
				if(fs[2] == 0){
					glColor4f(1,1,1,1); //rosa
					circle.draw2f(posx-0.005f, posz-0.005f, 0.01f, 0.01f);
				}
				else{
					if(fs[2] == 2){
						glColor3f(1,0.0f,0);
						circle.draw2f(posx-0.005f, posz-0.005f, 0.01f, 0.01f);
					}
					else{
						glColor3f(1,0.8f,0);
						GL11.glBegin(GL11.GL_TRIANGLES);
						GL11.glVertex2f(posx-0.005f, posz-0.005f);
						GL11.glVertex2f(posx+0.005f, posz-0.005f);
						GL11.glVertex2f(posx, posz+0.005f);					
						GL11.glEnd();
					}
				}
			//	g2d.fillOval(posx, posz, 10, 10);
				
			}
			
			
			glColor3f(1, 0.5f, 1); //rosa		
			GL11.glBegin(GL11.GL_TRIANGLES);
			GL11.glVertex2f(p2.x, p2.y);
			GL11.glVertex2f(p5.x, p5.y);
			GL11.glVertex2f(p6.x, p6.y);
			GL11.glEnd();				
		}
		
		
		//draw border
		glColor3f(0.5f, 0f, 0); //rosa		
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x, y);
		GL11.glVertex2f(x-0.005f, y);
		GL11.glVertex2f(x-0.005f, y+height);
		GL11.glVertex2f(x, y+height);
		GL11.glEnd();	
		
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x-0.005f, y+height);
		GL11.glVertex2f(x-0.005f, y+height+0.005f);
		GL11.glVertex2f(x+width, y+height+0.005f);
		GL11.glVertex2f(x+width, y+height);
		GL11.glEnd();	
		
	}

	float clamp(float X, float Min, float Max)
	{
	  if( X > Max )
	    X = Max;
	  else if( X < Min )
	    X = Min;
	 
	  return X;
	}
	
	class FloatPoint
	{
		float x,y;
		
		FloatPoint(float x, float y)
		{
			this.x = x;
			this.y = y;
		}
	}
}

